package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.OrderBack;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.service.OrderBackService;
import com.bwhx.eruler.service.RulerService;
import com.bwhx.eruler.util.StringUtils;
/**
 * 出库回退再入库
 * @author Cuilitang
 *
 */

@Controller
public class OrderBackController extends BaseController {
	
	@Autowired
	OrderBackService obService;
	@Autowired
	RulerService rulerService;
	
	@ResponseBody
  	@RequestMapping(value = "/page.orderBackList.ashx")
	 public Map orderList(Integer page,Integer rows,OrderBack ob) {
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,ob);
		Map map = new HashMap();
		try{
			List<OrderBack> list = obService.list(p);
			Integer count = obService.count(ob);

			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	@ResponseBody
	@RequestMapping(value = "/page.saveOrderBack.ashx")
	public String saveOrderBack(OrderBack ob) {
		
		try{
			ob.setoBTime(System.currentTimeMillis()/1000);
			ob.setoBIsDone(false);
			if(ob.getoBType()==0){
				ob.setoBBuyerId(ob.getoBProposerId());
			}
			
			obService.insert(ob);
		
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@RequestMapping(value = "/page.orderBackdetails.ashx")
	 public ModelAndView OfflineOrderDetails(@RequestParam String oBId){
		OrderBack ob = obService.get(oBId);
	    return new ModelAndView("/order/orderbackdetails.jsp","ob",ob);  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.processOrderBack.ashx")
	 public Map<String,String> processOrder(@RequestBody String b){
		log.info(b);
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("status", "1");
		
		OrderBack ob = new OrderBack();
		List<Ruler> rulers = new ArrayList<Ruler>();
		Map<String, String> paramMap = new HashMap<String, String>();	
		List<Ruler> paramRulers = new ArrayList<Ruler>();	
		
		String[] split = b.split("&");
		for (String string : split) {
			String[] keyValue = string.split("=");
			paramMap.put(keyValue[0], keyValue[1]);
		}
		
		Integer oBPNumber = Integer.parseInt(paramMap.get("oBPNumber"));
		String oBId = paramMap.get("oBId");
		String oBBuyerId = paramMap.get("oBBuyerId");
		String oBType = paramMap.get("oBType");
		/*String oBProposerId = map.get("oBProposerId");
		Integer oBType = Integer.parseInt(map.get("oBType"));
		*/
		
		ob.setoBBuyerId(Integer.parseInt(oBBuyerId));
		ob.setoBType(Integer.parseInt(oBType));
		
		
		
	
		ob.setoBId(Integer.parseInt(oBId));
		ob.setoBIsDone(true);//已完成
		
		ArrayList<String> opcList = new ArrayList<String>();
	
		//code和版本列表
		for(int i=1;i<=oBPNumber;i++){
			String opc=paramMap.get("opcode_"+i);
			String gowhere=paramMap.get("gowhere_"+i);
			
			
			if(opcList.contains(opc)){
				resultMap.put("status", "conflict");
				resultMap.put("RSN", opc);
				return resultMap;
			}else{
				opcList.add(opc);
				
				Ruler ruler = new Ruler(opc,Integer.parseInt(gowhere));
				paramRulers.add(ruler);
			}
		}
		for (String opc : opcList) {
			Ruler chuKuRuler = rulerService.findChuKuAndBaoFeiBySn(opc);
			if(null==chuKuRuler){
				resultMap.put("status", "nonentity");
				resultMap.put("RSN", opc);
				return resultMap;
			}else if(!chuKuRuler.getrBuyerId().equals(ob.getoBBuyerId())){
				resultMap.put("status", "notbelong");
				resultMap.put("RSN", opc);
				return resultMap;
			}
	
			rulers.add(chuKuRuler);
		}
		
		ob.setoBPCodes(opcList.toString().replace("[", "").replace("]", ""));

		obService.update(ob,rulers,paramRulers);
	
		return resultMap;  
	}
	
  @ResponseBody
  @RequestMapping(value = "/page.orderbackdel.ashx")
 public Map<String,String> OfflineOrderDel(@RequestParam String oBId){
	Map<String,String> resultMap = new HashMap<String, String>();
	resultMap.put("status", "1");
	obService.del(oBId);

    return resultMap;  
  }
	
	
}
